package dao

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"social-app/config"
	"social-app/pkg/logger"
	"time"
)

var (
	Db  *gorm.DB
	err error
)

func init() {
	Db, err = gorm.Open(mysql.Open(config.DdConfig), &gorm.Config{
		PrepareStmt: true,
	})
	if err != nil {
		logger.Error(map[string]interface{}{"mysql connect error": err.Error()})
		panic("failed to connect database")
	}
	if Db.Error != nil {
		logger.Error(map[string]interface{}{"mysql database error": Db.Error})
		panic("mysql database error")
	}
	sqlDB, _ := Db.DB()

	// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
	sqlDB.SetMaxIdleConns(10)

	// SetMaxOpenConns sets the maximum number of open connections to the database.
	sqlDB.SetMaxOpenConns(100)

	// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
	sqlDB.SetConnMaxLifetime(time.Hour)
}
